package com.songlanyun.modules.donationpoints.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.enums.DonationConstant;
import com.songlanyun.common.enums.IntegralConstant;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import com.songlanyun.modules.account.service.UserIdentityAssetService;
import com.songlanyun.modules.donationpoints.dao.DonationPointsDao;
import com.songlanyun.modules.donationpoints.entity.DonationPointsEntity;
import com.songlanyun.modules.donationpoints.service.DonationPointsService;
import com.songlanyun.modules.donationpointsconfig.entity.DonationPointsConfigEntity;
import com.songlanyun.modules.donationpointsconfig.service.DonationPointsConfigService;
import com.songlanyun.modules.integralrecord.service.IntegralRecordService;
import com.songlanyun.modules.publicbenefitmsg.model.DonateMsg;
import com.songlanyun.modules.publicbenefitmsg.model.to.PublicBenefitMsgTO;
import com.songlanyun.modules.publicbenefitmsg.service.PublicBenefitMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;


@Service("donationPointsService")
public class DonationPointsServiceImpl extends ServiceImpl<DonationPointsDao, DonationPointsEntity> implements DonationPointsService {

    @Resource
    private DonationPointsConfigService donationPointsConfigService;

    @Autowired
    private IntegralRecordService integralRecordService;

    @Override
    public PageUtils queryDataPage(Map<String, Object> params) {
        IPage<DonationPointsEntity> page = this.baseMapper.page(new Query<DonationPointsEntity>().getPage(params), params);
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Long donorId = MapUtil.getLong(params, "donor_id");
        IPage<DonationPointsEntity> page = this.baseMapper.selectPage(
                new Query<DonationPointsEntity>().getPage(params),
                new LambdaQueryWrapper<DonationPointsEntity>()
                        .eq(ObjectUtil.isNotNull(donorId), DonationPointsEntity::getDonorId, donorId)
                        .orderByDesc(DonationPointsEntity::getDonationTime)
        );
        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DonationPointsEntity donate(DonationPointsEntity donationPoints) {
        //查询捐赠项目
        DonationPointsConfigEntity config = donationPointsConfigService.loadById(donationPoints.getConfigId(), true);
//        //扣除公益积分明细
//        integralRecordService.outer(donationPoints.getDonorId(), config.getPoints(),
//                IntegralConstant.OuterMode.DONATION_POINTS, IntegralConstant.IntegralModelType.DONATION_POINTS, donationPoints.getId(),
//                "公益积分捐赠");

        //保存捐赠记录
        donationPoints.setDonationPoints(config.getPoints());
        donationPoints.setDonationTime(new Date());
        this.baseMapper.insert(donationPoints);
        return donationPoints;
    }

}
